package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.Broker;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.BrokerConsumeDetailMapper;
import com.rm.zeyou.mapper.BrokerMapper;
import com.rm.zeyou.mapper.TechnicianConsumeDetailMapper;
import com.rm.zeyou.mapper.TechnicianMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.BrokerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.vo.request.BrokerInvitationListVo;
import com.rm.zeyou.vo.response.BrokerIndexResponseVo;
import com.rm.zeyou.vo.response.BrokerInvitationListResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 经纪人表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:07
 */
@Service
public class BrokerServiceImpl extends ServiceImpl<BrokerMapper, Broker> implements BrokerService {
    @Resource
    private BrokerMapper brokerMapper;
    @Resource
    private BrokerConsumeDetailMapper brokerConsumeDetailMapper;
    @Resource
    private TechnicianMapper technicianMapper;
    @Resource
    private TechnicianConsumeDetailMapper technicianConsumeDetailMapper;

    @Override
    public ApiRestResponse<BrokerIndexResponseVo> brokerIndex(Technician technician) {
        if (technician.getTechnicianIdentity().equals(NumberConstants.ZERO_CONSTANTS)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.NOT_BROKER);
        }
        BrokerIndexResponseVo responseVo = new BrokerIndexResponseVo();

        BigDecimal freezePrice = technicianConsumeDetailMapper.selectAddAmount(technician.getId(), NumberConstants.ZERO_CONSTANTS);
        responseVo.setFreezePrice(freezePrice);

        //累计收入金额
        Broker broker = brokerMapper.selectOne(new LambdaQueryWrapper<Broker>().eq(Broker::getTid, technician.getId()));
        BigDecimal addAmount = brokerConsumeDetailMapper.selectAddAmount(broker.getId());
        //已提现金额
        BigDecimal payoutAmount = brokerConsumeDetailMapper.selectPayoutAmount(broker.getId());

        responseVo.setBrokerName(broker.getBrokerName());
        responseVo.setBrokeTel(broker.getBrokerTel());
        responseVo.setBrokerAvatar(technician.getTechnicianAvatar());
        responseVo.setCanPayoutPrice(addAmount.subtract(payoutAmount));
        responseVo.setIsPayoutPrice(payoutAmount);
        responseVo.setTotalPrice(addAmount);

        Integer todayCount = brokerConsumeDetailMapper.selectDealOrderCount(broker.getId(), NumberConstants.ONE_CONSTANTS);
        Integer totalCount = brokerConsumeDetailMapper.selectDealOrderCount(broker.getId(), NumberConstants.ZERO_CONSTANTS);
        responseVo.setTodayDealOrderCount(todayCount);
        responseVo.setTotalDealOrderCount(totalCount);

        Integer todayAddCount = technicianMapper.selectAddCount(broker.getBrokerInvitationCode(), NumberConstants.ONE_CONSTANTS);
        Integer totalAddCount = technicianMapper.selectAddCount(broker.getBrokerInvitationCode(), NumberConstants.ZERO_CONSTANTS);
        responseVo.setTodayTechnicianCount(todayAddCount);
        responseVo.setTotalTechnicianCount(totalAddCount);


        return ApiRestResponse.success(responseVo);
    }

    @Override
    public ApiRestResponse<PageInfo<BrokerInvitationListResponseVo>> brokerInvitationList(Technician technician, BrokerInvitationListVo requestVo) {
        return ApiRestResponse.success(PageHelper.startPage(requestVo.getPageNum(), requestVo.getPageSize()).doSelectPageInfo(() ->
                brokerMapper.selectBrokerInvitationList(technician.getTechnicianInvitationCode())
        ));
    }
}
